window.PartnerHelper = {
    init(param, cb) {
        wx.showShareMenu({ withShareTicket: true });
        wx.onShareAppMessage(() => {
            // 用户点击了“转发”按钮
            return {
                title: '转发标题'
            }
        })

        wx.onShow((res) => {
            G.partner.onShow(res);
        })

    },

    share(param) {
        wx.shareAppMessage({
            title: param.title,
            // imageUrl: param.imageUrl || canvas.toTempFilePathSync({
            //     x: 10,
            //     y: 10,
            //     width: 200,
            //     height: 150,
            //     destWidth: 400,
            //     destHeight: 300
            // })
        })
    },

    _getIsFitHeight() {
        if (!this.sysInfo) {
            this.sysInfo = wx.getSystemInfoSync();
        }
        let isFitHeight = false;
        let designScale = 720 / 1280;
        if (designScale <= this.sysInfo.screenWidth / this.sysInfo.screenHeight) {
            isFitHeight = true;
        }
        return isFitHeight;
    },

    _getLocalPix(num) {
        if (!this.sysInfo) {
            this.sysInfo = wx.getSystemInfoSync();
        }
        let isFitHeight = this._getIsFitHeight();
        let scale;
        if (isFitHeight) {
            scale = 1280 / (this.sysInfo.screenHeight * this.sysInfo.pixelRatio);
        } else {
            scale = 720 / (this.sysInfo.screenWidth * this.sysInfo.pixelRatio);
        }

        return num / (scale * this.sysInfo.pixelRatio);
    },

    _getLocalX(x) {
        if (!this.sysInfo) {
            this.sysInfo = wx.getSystemInfoSync();
        }

        let localOffsetX = this._getLocalPix(x);
        return this.sysInfo.screenWidth / 2 + localOffsetX;
    },
    _getLocalY(y) {
        if (!this.sysInfo) {
            this.sysInfo = wx.getSystemInfoSync();
        }

        let localOffsetY = this._getLocalPix(y);
        return this.sysInfo.screenHeight / 2 - localOffsetY;
    },

    createGameClub() {
        if (this.btnGameClub) {
            this.showGameClub();
            return;
        }
        let x = -295;
        let y = -162;
        let width = 80;
        let height = 80;
        this.btnGameClub = wx.createGameClubButton({
            icon: '',
            style: {
                left: this._getLocalX(x - width / 2),
                top: this._getLocalY(y + height / 2),
                width: this._getLocalPix(width),
                height: this._getLocalPix(height)
            }
        })
    },
    showGameClub() {
        if (!this.btnGameClub) {
            return;
        }
        this.btnGameClub.show();
    },
    hideGameClub() {
        if (!this.btnGameClub) {
            return;
        }
        this.btnGameClub.hide();
    },

    postMessage(k, v) {
        let openDataContext = wx.getOpenDataContext()
        openDataContext.postMessage({
            key: k,
            param: v
        })
    },

    setUserCloudStorage(k, v, cb) {
        wx.setUserCloudStorage({
            KVDataList: [{ key: parseStr(k), value: parseStr(v) }],
            success: function () {
                console.log('setUserCloudStorage:success' + v);
                cb && cb();
            },
            fail: function () {
                console.log('setUserCloudStorage:fail' + v);
                cb && cb(true);
            }
        });
    },

    vibrateShort() {
        wx.vibrateShort();
    },

    vibrateLong() {
        wx.vibrateLong();
    },

    navigateToMiniProgram(appid){
        wx.navigateToMiniProgram({
            appId: appid,
            path: '',
            success(res) {
              // 打开成功
            }
          })
    },
}